home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_06 / 2n06050a < prev    next >
Text File  |  1991-04-30  |  23KB  |  902 lines

  1. /**********************************************************
  2.     PURPOSE: Links with a Standard C program to
  3.              provide a Windows Application envirnment.
  4.  
  5.  
  6.     FUNCTIONS:
  7.  
  8.         WinMain() - calls initialization function,
  9.             processes message loop
  10.         StdWinInit() - initializes window data and
  11.             registers window
  12.         StdWinWndProc() - processes messages
  13.         About() - processes messages for "About" dialog box
  14.         SelectFont() - select a font
  15.         GetSizes() - get size of current font
  16.         GetFonts() - get available fonts
  17.         SetMyDC() - initializes DC
  18.  
  19.     COMMENTS:   This code started as the Showfont sample
  20.         software that is included in the Windows Software
  21.         Development Kit. Compile this file as a small or
  22.         medium model with the following options:
  23.             CL /A[S or M] /c /Gsw /Oas /Zp -Zi STDWIN.C
  24.  
  25.         The Zi option is only if you are able to use
  26.         CodeView for Windows.
  27.  
  28.         When you link, you must link with the C6.0 linker
  29.         (Version 5.1 or greater), or link4 to get the proper
  30.         exe. You must link with the libraries that come with
  31.         the Windows Software Development kit, not the C
  32.         compiler libraries. These libraries are:
  33.             LIBW.LIB and SLIBCEW.LIB or MLIBCEW.LIB
  34.  
  35.         After linking you must resource compile the
  36.         executable with the resource script. First:
  37.             rc -r stdwin.rc
  38.  
  39.         After linking combine the binary resource file
  40.         with the exe by:
  41.             rc stdwin.res
  42. ***********************************************************/
  43.  
  44. #include "windows.h"
  45. #include "StdWin.h"
  46. #include <string.h>
  47. #include <stdio.h>
  48.  
  49.  
  50.  
  51. /* set this to your own title */
  52. char WindowTitle[] = "Grep";
  53. WORD LineExtents[MAXROWS+2];
  54. HANDLE hInst,hStdWinWnd;
  55. HDC SetMyDC(HDC) ;
  56. HFONT hFont1;
  57. HFONT hUnderlineFont = 0,hBoldFont = 0,hItalicFont;
  58. char FontNameList[32][128];     /* list of added fonts  */
  59. int nFontIndex = 0;             /* position in FontList */
  60. int  BreakSet;                  /* break key flag */
  61. BYTE KeyBuffer[64],KeyHead = 0,KeyTail = 0,KeyMask = 0x3f;
  62.  /* storage for screen text */
  63. SCREEN ScreenBuffer[MAXROWS+2][MAXCOLUMNS];
  64. int VisibleRows,VisibleCols,TopRow,LeftCol,VertScrollInc,
  65.     HorizScrollInc;
  66. TEXTMETRIC TextMetric;
  67. int AveCharacterWidth;
  68. LOGFONT LogFont;
  69. short nBkMode = OPAQUE;
  70. DWORD rgbBkColor = RGB(255, 255, 255);
  71. DWORD rgbTextColor = RGB(0, 0, 0);
  72. DWORD rgbColor;
  73. short nAlignLCR = TA_LEFT;
  74. short nAlignTBB = TA_TOP; 
  75. char FontList[MAXFONT][32];
  76. BYTE CharSet[MAXFONT];
  77. BYTE PitchAndFamily[MAXFONT];
  78. int FontIndex = 0;
  79. int SizeList[MAXSIZE];
  80. int SizeIndex = 0;
  81. int CurrentFont = 0;
  82. int CurrentSize = 0;
  83. int xCurrentPos=0,yCurrentPos =0;
  84. FARPROC lpColors;
  85. FARPROC lpSelectFont;
  86. FARPROC lpEnumFunc;
  87. CATCHBUF   CatchBuf;
  88. short DisplayCol = 0,DisplayLine = 0;
  89. RECT Rect;  /* dimension of the client window  */
  90. BYTE *ptr,*ptr2;
  91.  
  92. /*********************************************************
  93.  
  94.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  95.  
  96.     PURPOSE: calls initialization function,
  97.         processes message loop
  98.  
  99. ***********************************************************/
  100.  
  101. int PASCAL WinMain(hInstance, hPrevInstance,
  102.     lpCmdLine, nCmdShow)
  103. HANDLE hInstance;
  104. HANDLE hPrevInstance;
  105. LPSTR lpCmdLine;
  106. int nCmdShow;
  107. {
  108.     HWND hWnd;
  109.     MSG msg;
  110.  
  111.     if (!hPrevInstance)
  112.         if (!StdWinInit(hInstance))
  113.             return (FALSE);
  114.  
  115.     hInst = hInstance;
  116.  
  117.     hWnd = CreateWindow("StdWin",
  118.         WindowTitle,
  119.         WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  120.         CW_USEDEFAULT,
  121.         CW_USEDEFAULT,
  122.         CW_USEDEFAULT,
  123.         CW_USEDEFAULT,
  124.         NULL,
  125.         NULL,
  126.         hInstance,
  127.         NULL);
  128.  
  129.     if (!hWnd)
  130.         return (FALSE);
  131.     hStdWinWnd = hWnd;
  132.     ShowWindow(hWnd, nCmdShow);
  133.     UpdateWindow(hWnd);
  134.  
  135.     while(PeekMessage(&msg,NULL,0,0xFFFF,PM_REMOVE)){
  136.         TranslateMessage(&msg);
  137.         DispatchMessage(&msg);
  138.     }
  139.     /* store process state for ctrl-c break */
  140.     if(!Catch(CatchBuf))
  141.         main();
  142.     MessageBox(hWnd,"Program Terminated Normally",
  143.         WindowTitle,MB_OK);
  144.     if(hOutFile)
  145.         _lclose(hOutFile);
  146.     hOutFile = Redirected = 0;
  147.  
  148.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  149.         TranslateMessage(&msg);
  150.         DispatchMessage(&msg);}
  151.  
  152.     return (msg.wParam);
  153. }
  154.  
  155. /*********************************************************
  156.  
  157.     FUNCTION: StdWinInit(HANDLE)
  158.  
  159.     PURPOSE: Initializes window data and registers class
  160.  
  161. ***********************************************************/
  162.  
  163. int StdWinInit(hInstance)
  164. HANDLE hInstance;
  165. {
  166.     HANDLE hMemory;
  167.     PWNDCLASS pWndClass;
  168.     BOOL bSuccess;
  169.  
  170.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  171.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  172.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  173.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  174.     pWndClass->lpszMenuName = (LPSTR) "StdWin";
  175.     pWndClass->lpszClassName = (LPSTR) "StdWin";
  176.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  177.     pWndClass->hInstance = hInstance;
  178.     pWndClass->style = NULL;
  179.     pWndClass->lpfnWndProc = StdWinWndProc;
  180.  
  181.     bSuccess = RegisterClass((LPWNDCLASS) pWndClass);
  182.  
  183.     LocalUnlock(hMemory);
  184.     LocalFree(hMemory);
  185.     return (bSuccess);
  186. }
  187.  
  188. /**********************************************************
  189.  
  190.     FUNCTION: SetMyDC(HDC)
  191.  
  192.     PURPOSE: Initializes the DC
  193.  
  194. ***********************************************************/
  195.  
  196. HDC SetMyDC(hDC) 
  197. HDC hDC;
  198. {
  199.     SetBkMode(hDC, nBkMode);
  200.     SetBkColor(hDC, rgbBkColor);
  201.     SetTextColor(hDC, rgbTextColor);
  202.     SetTextAlign(hDC, nAlignLCR | nAlignTBB);
  203.     SelectObject(hDC,hFont1);
  204.     GetTextMetrics(hDC,&TextMetric);
  205. }
  206.  
  207.  
  208.  
  209. /**********************************************************
  210.  
  211.     FUNCTION: Colors(HWND, unsigned, WORD LONG)
  212.  
  213.     PURPOSE: Dialog box for changing text background color
  214.  
  215. ***********************************************************/
  216.  
  217. BOOL FAR PASCAL Colors(hDlg, message, wParam, lParam)
  218. HWND hDlg;
  219. unsigned message;
  220. WORD wParam;
  221. LONG lParam;
  222. {
  223.     int Red, Green, Blue;
  224.  
  225.     switch (message) {
  226.         case WM_INITDIALOG:
  227.             SetDlgItemInt(hDlg, ID_RED,
  228.                 GetRValue(rgbColor), FALSE);
  229.             SetDlgItemInt(hDlg, ID_GREEN,
  230.                 GetGValue(rgbColor), FALSE);
  231.             SetDlgItemInt(hDlg, ID_BLUE,
  232.                 GetBValue(rgbColor), FALSE);
  233.             return (TRUE);
  234.             break;
  235.  
  236.         case WM_COMMAND:
  237.             switch (wParam) {
  238.                 case IDOK:
  239.                     Red = GetDlgItemInt(hDlg, ID_RED,
  240.                         NULL, FALSE);
  241.                     Green = GetDlgItemInt(hDlg, ID_GREEN,
  242.                         NULL, FALSE);
  243.                     Blue = GetDlgItemInt(hDlg, ID_BLUE,
  244.                         NULL, FALSE);
  245.                     rgbColor = RGB(Red, Green, Blue);
  246.                     EndDialog(hDlg, 1);
  247.                     break;
  248.  
  249.                 case IDCANCEL:
  250.                     EndDialog(hDlg, 0);
  251.                     break;
  252.             }
  253.             break;
  254.     }
  255.     return (FALSE);
  256. }
  257.  
  258. /********************************************************
  259.  
  260.     FUNCTION: EnumFunc(LPLOGFONT,LPTEXTMETRIC,short,LPSTR)
  261.  
  262. **********************************************************/
  263.  
  264. int FAR PASCAL EnumFunc(lpLogFont, lpTextMetric,
  265.     FontType, lpData)
  266. LPLOGFONT lpLogFont;
  267. LPTEXTMETRIC lpTextMetric;
  268. short FontType;
  269. LPSTR lpData;
  270. {
  271.     switch (LOWORD(lpData)) {
  272.         case 0:
  273.             if (FontIndex >= MAXFONT)
  274.                 return (0);
  275.             _fstrcpy((LPSTR) FontList[FontIndex],
  276.                 (LPSTR) (lpLogFont->lfFaceName));
  277.             CharSet[FontIndex] = lpLogFont->lfCharSet;
  278.             PitchAndFamily[FontIndex] =
  279.                 lpLogFont->lfPitchAndFamily;
  280.             return (++FontIndex);
  281.  
  282.         case 1:
  283.             if (SizeIndex >= MAXSIZE)
  284.                 return (0);
  285.             SizeList[SizeIndex] = lpLogFont->lfHeight;
  286.             return (++SizeIndex);
  287.     }
  288. }
  289.  
  290. /*******************************************************
  291.  
  292.     FUNCTION: GetFonts(HWND)
  293.  
  294.     PURPOSE: Get available fonts
  295.  
  296. *********************************************************/
  297.  
  298. void GetFonts(hWnd)
  299. HWND hWnd;
  300. {
  301.  
  302.     HDC hDC;
  303.  
  304.     FontIndex = 0;
  305.     SizeIndex = 0;
  306.     hDC = GetDC(hWnd);
  307.     lpEnumFunc = MakeProcInstance(EnumFunc, hInst);
  308.     EnumFonts(hDC, (LPSTR) NULL, lpEnumFunc, (LPSTR) NULL);
  309.     FreePr